package com.kylin.common.security.handler;

import com.kylin.common.core.beans.GlobalResultCode;
import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.security.exception.AuthErrorMessage;
import com.kylin.common.security.util.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 认证失败异常处理
 * @author wuhao
 * @version 1.0 - 2023/1/8
 */
@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
//        throw new BaseException(GlobalResultCode.UNAUTHORIZED.getCode(), AuthErrorMessage.AUTHENTICATION_FAILED.getMessage());
        ResultBean<Serializable> resultBean = new ResultBean<>(GlobalResultCode.UNAUTHORIZED.getCode(), AuthErrorMessage.AUTHENTICATION_FAILED.getMessage());
        ResponseUtils.out(response, resultBean);
    }
}
